﻿using System;

namespace FunctionalCLR.Core.Function.Flow {
    public class SwitchNode : FuncNode {

        public SwitchNode()
            : base() {
            base.Value = "case";
        }

        public override FuncNode GetReversedFunction(int index) {
            throw new NotImplementedException();
        }

        public override object Evaluate() {
            int count = Children.Count;
            object condition = Children[count - 1].Evaluate();

            for (int index = count - 2; index > 0; index = index - 2) {
                object value = Children[index - 1].Evaluate();
                if (value == condition) {
                    return Children[index].Evaluate();
                }
            }

            return null;
        }
    }
}
